package elements.axis; 
	
import flash.display.Sprite;
import flash.text.TextField;
import flash.geom.Rectangle;

class AxisLabel extends TextField {
	
	public var xAdj:Float ;
	public var yAdj:Float ;
	public var leftOverhang:Float ;
	public var rightOverhang:Float ;
	public var xVal:Float ;
	public var yVal:Float ;
	
	public function new()	{
		xAdj = 0;
		yAdj = 0;
		leftOverhang = 0;
		rightOverhang = 0;
		xVal = Math.NaN;
		yVal = Math.NaN;
		super();
	}
	
	/**
		 Rotate the label and align it to the X Axis tick	 
		 @param	rotation
	*/
	public function rotate_and_align( rotation:Float, align:String, parent:Sprite ): Void
	{ 
		rotation = rotation % 360;
		if (rotation < 0) rotation += 360;
		
		var myright:Float = this.width * Math.cos(rotation * Math.PI / 180);
		var myleft:Float = this.height * Math.cos((90 - rotation) * Math.PI / 180);
		var mytop:Float = this.height * Math.sin((90 - rotation) * Math.PI / 180);
		var mybottom:Float = this.width * Math.sin(rotation * Math.PI / 180);
		
		if (((rotation % 90) == 0) || (align == "center"))
		{
			this.xAdj = (myleft - myright) / 2;
		}
		else
		{
			this.xAdj = (rotation < 180) ? myleft / 2 : -myright + (myleft / 2);
		}

		if (rotation > 90) {
			this.yAdj = -mytop;
		}
		if (rotation > 180) {
			this.yAdj = -mytop - mybottom;
		}
		if (rotation > 270) {
			this.yAdj = - mybottom;
		}
		this.rotation = rotation;

		var titleRect:Rectangle = this.getBounds(parent);
		this.leftOverhang = Math.abs(titleRect.x + this.xAdj);
		this.rightOverhang = Math.abs(titleRect.x + titleRect.width + this.xAdj);
	}
}
